﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using SHomeWorkshop.LunarTools;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class PictureBoxBorderAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示PictureBox选取状态的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入PictureBox的“Image”主子元素。</param>
        public PictureBoxBorderAdorner(UIElement adornedElement, PictureBox masterPictureBox)
            : base(adornedElement)
        {
            this.masterPictureBox = masterPictureBox;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
            this.MouseDown += new System.Windows.Input.MouseButtonEventHandler(PictureBoxBorderAdorner_MouseDown);
            this.MouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(PictureBoxBorderAdorner_MouseRightButtonUp);
        }

        void PictureBoxBorderAdorner_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            PictureBox.S_ContextMenu.IsOpen = true;
            e.Handled = true;
        }

        void PictureBoxBorderAdorner_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (masterPictureBox != null)
            {
                masterPictureBox.SelectOnlySelf();
                e.Handled = true;
            }
        }

        private PictureBox masterPictureBox;

        public PictureBox MasterPictureBox
        {
            get { return masterPictureBox; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (masterPictureBox == null) return;

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            //先画线。
            Pen linePen = null;

            if (this.masterPictureBox != null && this.masterPictureBox.Editor != null)
            {
                Brush borderColor = masterPictureBox.BorderColor;

                linePen = new Pen(Brushes.Blue, 1);
                //linePen = this.masterTitle.Editor.SubPanelSelectionAdornerPen;
                linePen.DashStyle = DashStyles.DashDot;

                linePen.LineJoin = PenLineJoin.Round;
                linePen.StartLineCap = PenLineCap.Round;
                linePen.EndLineCap = PenLineCap.Round;

                //Point newTopLeft = new Point(adornedElementRect.TopLeft.X - 18, adornedElementRect.TopLeft.Y - 18);
                //Point ptTmp = new Point(adornedElementRect.BottomRight.X + 18, adornedElementRect.BottomRight.Y + 18);

                Point newTopLeft = new Point(adornedElementRect.TopLeft.X - 8, adornedElementRect.TopLeft.Y - 8);
                Point ptTmp = new Point(adornedElementRect.BottomRight.X + 8, adornedElementRect.BottomRight.Y + 8);
                Rect selectedRect = new Rect(newTopLeft, ptTmp);

                //反正要画边框，不管是否选中。

                Pen borderPen = new Pen(borderColor, 1);
                Rect outBorderRect = new Rect(adornedElementRect.Left - 10, adornedElementRect.Top - 10,
                        adornedElementRect.Width + 20, adornedElementRect.Height + 20);

                //drawingContext.DrawRectangle(null, borderPen, outBorderRect);//必须用null，否则会导致无法拖动。

                drawingContext.DrawRectangle(null, borderPen,                //必须用null，否则会导致无法拖动。
                    new Rect(adornedElementRect.Left - 5, adornedElementRect.Top - 5,
                        adornedElementRect.Width + 10, adornedElementRect.Height + 10)
                        );

                //画顶点小角.左上
                //drawingContext.DrawRectangle(borderColor, borderPen,
                //    new Rect(outBorderRect.Left - 5, outBorderRect.Top - 5, 5, 5));
                //drawingContext.DrawRectangle(borderColor, borderPen,
                //    new Rect(outBorderRect.Left - 5, outBorderRect.Top + 5, 5, 5));
                //drawingContext.DrawRectangle(borderColor, borderPen,
                //    new Rect(outBorderRect.Left + 5, outBorderRect.Top - 5, 5, 5));

                //画顶点小角.右下
                //drawingContext.DrawRectangle(borderColor, borderPen,
                //    new Rect(outBorderRect.Right, outBorderRect.Bottom, 5, 5));
                //drawingContext.DrawRectangle(borderColor, borderPen,
                //    new Rect(outBorderRect.Right, outBorderRect.Bottom - 10, 5, 5));
                //drawingContext.DrawRectangle(borderColor, borderPen,
                //    new Rect(outBorderRect.Right - 10, outBorderRect.Bottom, 5, 5));

                if (masterPictureBox.IsSelected)
                {

                    //使用这个办法其实比画四条线更好。但要注意，第一个参数必须为：null而不能是：Brshes.Transparent。
                    //否则画出来的矩形不是空心的。
                    //由于公式需要嵌套，一旦矩形不是空心，则选中外层的公式后，
                    //就不能再用鼠标选中内层的公式了。
                    drawingContext.DrawRectangle(null, linePen, selectedRect);

                    //再画点。
                    //SolidColorBrush renderBrush;
                    Brush renderBrush;
                    Pen renderPen;

                    if (this.masterPictureBox != null && this.masterPictureBox.Editor != null)
                    {
                        renderPen = new Pen(Brushes.Blue, 1);
                        //renderPen = this.masterTitle.Editor.ExSelectionAdornerPen;

                        renderBrush = Brushes.Blue;
                        //renderBrush = this.masterTitle.Editor.ExSelectionAdornerBrush;
                    }
                    else
                    {
                        renderPen = null;
                        renderBrush = null;
                    }

                    drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                        new Rect(selectedRect.Left - 2, selectedRect.Top - 2, 4, 4));//左上
                    drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                        new Rect(selectedRect.Right - 2, selectedRect.Top - 2, 4, 4));//右上
                    drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                        new Rect(selectedRect.Left - 2, selectedRect.Bottom - 2, 4, 4));//左下
                    drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                        new Rect(selectedRect.Right - 2, selectedRect.Bottom - 2, 4, 4));//右下
                }
            }
        }
    }
}
